/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_combat_options.cpp


	$Header: /game/dialog_combat_options.cpp $

	$NoKeywords: $

 ************************************************************************/
#include "precompiled.h"
#include "dialog_combat_options.h"

#include "adaptor_handler.h"
#include "adventure_frame.h"
#include "basic_dialog.h"
#include "bitmap_group_cache.h"
#include "bitmap_group_ptr.h"
#include "bitmap_group.h"
#include "bitmap_layer_cache_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "combat_context.h"
#include "combat_window.h"
#include "external_string.h"
#include "game_application.h"
#include "interface_help.h"
#include "options.h"
#include "standard_fonts.h"

extern t_button_cache g_large_button;
static t_bitmap_group_cache const k_bitmaps( "dialog.combat.options" );
static t_external_string const k_text_title( "combat_options_title.misc" );
static t_external_string const k_text_new_game( "combat_options_new.misc" );
static t_external_string const k_text_load_game( "combat_options_load.misc" );
static t_external_string const k_text_save_game( "combat_options_save.misc" );
static t_external_string const k_text_quit_game( "combat_options_quit.misc" );
static t_external_string const k_text_show_damage( "combat_options_show_damage_numbers.misc" );
static t_external_string const k_text_show_deaths( "combat_options_show_death_numbers.misc" );
static t_external_string const k_text_show_other_text( "combat_options_show_other_numbers.misc" );
static t_external_string const k_text_show_movement_shading( "combat_options_show_movement_shading.misc" );
static t_external_string const k_text_show_combat_grid( "combat_options_show_combat_grid.misc" );

extern t_external_string const k_text_save_on_quit;
extern t_external_string const k_text_cancel_on_quit;
extern t_external_string const k_text_quit_on_quit;
extern t_external_string const k_text_quit;

// ---------------------------------------------------------------------
// options dialog.  
// ---------------------------------------------------------------------
t_dialog_combat_options::t_dialog_combat_options( t_combat_window* parent )
                       : t_dialog_options_base( parent, k_bitmaps.get() )
{
	t_button* button;
	t_bitmap_layer const*	layer;
	t_button_handler		handler;
	t_help_block const& help = get_help_block( "combat_options" );

	m_combat_window = parent;
    
    // Title text.
	t_screen_rect rect;
	t_text_window* text_window;
	rect = m_bitmaps->find( "title" )->get_rect();
	text_window = new t_text_window( get_font( rect.height() ), rect, m_background,
		                             k_text_title, t_pixel_24(0,0,0));
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );
	text_window->set_center_horizontal();

	layer = m_bitmaps->find( "new_game_button" );
	button = new t_button( g_large_button.get(), layer->get_rect().top_left(), m_background,
		                   k_text_new_game );
	handler = bound_handler( *this, &t_dialog_combat_options::new_game_clicked );
	button->set_click_handler( handler );
   	set_help( button, help, "new" );

	layer = m_bitmaps->find( "load_game_button" );
	button = new t_button( g_large_button.get(), layer->get_rect().top_left(), m_background,
		                   k_text_load_game );
	handler = bound_handler( *this, &t_dialog_combat_options::load_game_clicked );
	button->set_click_handler( handler );
   	set_help( button, help, "load" );

	layer = m_bitmaps->find( "save_game_button" );
	button = new t_button( g_large_button.get(), layer->get_rect().top_left(), m_background,
		                   k_text_save_game );
	handler = bound_handler( *this, &t_dialog_combat_options::save_game_clicked );
	button->set_click_handler( handler );
   	set_help( button, help, "save" );

	layer = m_bitmaps->find( "quit_game_button" );
	button = new t_button( g_large_button.get(), layer->get_rect().top_left(), m_background,
		                   k_text_quit_game );
	handler = bound_handler( *this, &t_dialog_combat_options::quit_game_clicked );
	button->set_click_handler( handler );
   	set_help( button, help, "quit" );

	// set deaths / damage / other text
	handler = t_button_handler();

	layer = m_bitmaps->find( "show_damage_checkbox" );
	m_show_damage_button = add_checkbox( "show_damage", handler, get_show_combat_damage(), 
										 k_text_show_damage );
	m_show_deaths_button = add_checkbox( "show_death", handler, get_show_combat_deaths(),
										 k_text_show_deaths );
	m_show_other_text_button = add_checkbox( "show_miscellaneous", handler,
											 get_show_other_combat_text(), k_text_show_other_text );
	m_show_movement_shading_button = add_checkbox( "show_movement_grid", handler,
												    get_show_combat_movement_shading(), k_text_show_movement_shading,
													&help);
	m_show_combat_grid_button = add_checkbox( "show_grid", handler,
											  get_show_combat_grid(), k_text_show_combat_grid,
											  &help);
}

void t_dialog_combat_options::new_game_clicked( t_button* button )
{
	t_window_ptr ref = this;

	ok_clicked( button );
	m_combat_window->new_game();
}

void t_dialog_combat_options::load_game_clicked( t_button* button )
{
	t_window_ptr ref = this;

	ok_clicked( button );
	m_combat_window->load_game();
}

// ---------------------------------------------------------------------
// Save button on the quit subdialog was clicked
// ---------------------------------------------------------------------
void t_dialog_combat_options::quit_dialog_quit_and_save_clicked( t_basic_dialog * quit_dialog, t_button* button )
{
	t_window_ptr ref = this;

	quit_dialog->close();
	ok_clicked( button );

	if ( m_combat_window->save_game() )
	{
		if (   m_combat_window != NULL
			&& m_combat_window->get_context() != NULL
			&& m_combat_window->get_context()->get_adventure_frame() != NULL )
		{
			m_combat_window->get_context()->get_adventure_frame()->do_quit( button );
		}
		else
		{
			t_game_application::exit( 0 );
		}
	}
}

// ---------------------------------------------------------------------
// Quit button on the quit subdialog was clicked
// ---------------------------------------------------------------------
void t_dialog_combat_options::quit_dialog_quit_clicked( t_basic_dialog * quit_dialog, t_button* button )
{
	t_window_ptr ref = this;

	quit_dialog->close();
	ok_clicked( button );

	if (   m_combat_window != NULL
		&& m_combat_window->get_context() != NULL
		&& m_combat_window->get_context()->get_adventure_frame() != NULL )
	{
		m_combat_window->get_context()->get_adventure_frame()->do_quit( button );
	}
	else
	{
		t_game_application::exit( 0 );
	}
}

void t_dialog_combat_options::quit_game_clicked( t_button* button )
{
	t_window_ptr ref = this;

	t_help_block const&     help = get_help_block( "combat_options" );
	t_basic_dialog * quit_dialog = new t_basic_dialog;

	t_button_handler handler;

	t_button* quit_button = new t_button( g_large_button.get(), t_screen_point(0,0), quit_dialog, k_text_quit_on_quit );
	handler = add_1st_argument( bound_handler( *this, &t_dialog_combat_options::quit_dialog_quit_clicked ), quit_dialog );
	quit_button->set_click_handler( handler );
    set_help( quit_button, help, "quit_dialog_quit" );
	quit_dialog->add_button( quit_button );

	// Add the save and quit button
	t_button* save_button = new t_button( g_large_button.get(), t_screen_point(0,0), quit_dialog, k_text_save_on_quit );
	handler = add_1st_argument( bound_handler( *this, &t_dialog_combat_options::quit_dialog_quit_and_save_clicked ), quit_dialog );
	save_button->set_click_handler( handler );
    set_help( save_button, help, "quit_dialog_save_and_quit" );
	quit_dialog->add_button( save_button );

	// Add cancel button
	t_button* cancel_button = new t_button( g_large_button.get(), t_screen_point(0,0), quit_dialog, k_text_cancel_on_quit );
	quit_dialog->add_close_button( cancel_button, 0 );
    set_help( cancel_button, help, "quit_dialog_cancel" );

	quit_dialog->set_title( k_text_quit );
	quit_dialog->open();

	quit_dialog->run_modal();
}

void t_dialog_combat_options::save_game_clicked( t_button* button )
{
	t_window_ptr ref = this;

	ok_clicked( button );
	m_combat_window->save_game();
}

void t_dialog_combat_options::ok_clicked( t_button* button )
{
	set_show_combat_damage( m_show_damage_button->is_pressed() );
	set_show_combat_deaths( m_show_deaths_button->is_pressed() );
	set_show_other_combat_text( m_show_other_text_button->is_pressed() );
	set_show_combat_movement_shading( m_show_movement_shading_button->is_pressed() );
	set_show_combat_grid( m_show_combat_grid_button->is_pressed() );
	t_dialog_options_base::ok_clicked( button );
}
